package cn.chitanda.weather.ui.Weather

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.location.LocationManager
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import cn.chitanda.weather.data.WeatherRepository
import cn.chitanda.weather.data.db.PlaceDataBase
import cn.chitanda.weather.data.db.PlaceRepository
import cn.chitanda.weather.data.network.NetWorkService
import cn.chitanda.weather.data.weather.Basic
import cn.chitanda.weather.data.weather.Weather
import cn.chitanda.weather.utile.LocationUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class WeatherViewModel(application: Application) : AndroidViewModel(application) {
    private val dao = PlaceDataBase.getInstance(application.applicationContext).getPlaceDao()
    private val repository = WeatherRepository.getInstance(NetWorkService.getInstance())
    private val placeRepository = PlaceRepository.getInstance(dao, NetWorkService.getInstance())

    private var _cityList = placeRepository.getAllCity()
    val cityList: LiveData<List<Basic>> get() = _cityList

    private val _locationWeather = MutableLiveData<Weather>()
    val locationWeather: LiveData<Weather> get() = _locationWeather

    private val _weatherList = MutableLiveData<List<Weather>>()
    val weatherList: LiveData<List<Weather>> get() = _weatherList

    private suspend fun getWeather(): Weather? {
        _locationWeather.value = repository.refreshWeather(getLocation() ?: return null)
        return locationWeather.value!!
    }
//
//    suspend fun getCurrentCity() {
//        placeRepository.searchCity()
//    }

    suspend fun getCityList() {
        _cityList = withContext(Dispatchers.IO) {
            placeRepository.getAllCity()
        }
    }

    suspend fun getWeatherList() {
        val list: MutableList<Weather> = arrayListOf()
        if (locationWeather.value == null) {
            getWeather().also {
                if (it != null) {
                    list.add(it)
                }
            }
        } else {
            list.add(locationWeather.value!!)
        }
        if (cityList.value != null) {
            for (i in cityList.value!!) {
                val weather = repository.refreshWeather(i.cid)
                list.add(weather)
            }
        }

        _weatherList.value = list
    }

    suspend fun getWeatherList(cityList: List<Basic>) {
        val weatherList: MutableList<Weather> = arrayListOf()
        if (locationWeather.value == null) {
            getWeather()?.let { weatherList.add(it) }
        } else {
            weatherList.add(locationWeather.value!!)
        }
        for (i in cityList) {
            val weather = withContext(Dispatchers.Default) {
                repository.refreshWeather(i.cid)
            }
            weatherList.add(weather)
        }
        _weatherList.value = weatherList
    }

    @SuppressLint("MissingPermission")
    fun getLocation(): String? {
        val location = LocationUtil.getInstance(getApplication()).showLocation()
        return "${location?.latitude},${location?.longitude}"
    }

//
//    private fun launch(block: suspend () -> Unit, error: suspend (Throwable) -> Unit) =
//        viewModelScope.launch {
//            try {
//                block()
//            } catch (e: Throwable) {
//                error(e)
//            }
//        }
}
